<?php
    include '../JHeader.php';

    $id = -1;
    if(isset($_GET['id']))
    {
        $id = $_GET['id'];
    }

    function GoBackOnError($goto)
    {
        echo '<script type="text/javascript">timedRefresh(1500);</script>';

        //Falls JavaScript aus ist
        echo '<a href="index.php?id=' . $goto . '">Back</a>';
    }

    if(isset($_POST['registered']))
    {
        $name     = $_POST['team_name'];
        $shortcut = $_POST['team_shortcut'];
        $pass     = $_POST['team_pw'];

        require_once('../Database/Database.php');
        $db = new Database();
        $db->Connect();
        // Team existiert?
        if($db->SelectColumns($shortcut . '_teams', 'ID_Team', 'Name = \'' . $name . '\''))
        {
            echo 'A team with the same name already exists.<br />';
            GoBackOnError($id);
        }
        else
        {
            if($jid > 0)
            {
                // Team hinzufügen
                $columnArray = array('Name', 'Password');
                $valueArray  = array($name , $pass);

                $success = $db->Insert($shortcut . '_teams', $columnArray, $valueArray);
                $db->Disconnect();

                 if($success)
                 {
                     header('Location: index.php?id=' . $id);
                 }
                 else
                 {
                    echo 'Error: Could not add the team to the database.';
                    GoBackOnError($id);
                 }
            }
            else
            {
                echo 'Error: Could not find your ID in the database.';
                GoBackOnError($id);
            }
        }
    }
    else if($id > 0)
    {
        $id = $_GET['id'];
        require_once('../Database/Database.php');
        $db = new Database();
        $db->Connect();
        $shortcut = $db->SelectColumns('Series', 'Shortcut', 'ID_Series = ' . $id);
        $db->Disconnect();
        if($shortcut)
        {
            $shortcut = $shortcut[0]->Shortcut;
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <script type="text/javascript" language="JavaScript" src="js/check.js"></script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Neues Team - <?php echo $shortcut ?></title>
        <script type="text/javascript">
        function validate()
        {
            if (document.team_creation.team_name.value == '')
            {
                alert('Please enter a team name.');
                document.team_creation.team_name.focus();
                return false;
            }
            else if(document.team_creation.team_pw.value == '')
            {
                alert('Please enter a password.');
                document.team_creation.team_pw.focus();
                return false;
            }
        }

        function timedRefresh(timeInMs)
        {
            setTimeout('location.reload(true);', timeInMs);
        }
</script>
    </head>
    <body>
        <form name="team_creation" action="<?php echo $SERVER['SELF'] ?>" onsubmit="return validate()" method="post">
            <input type="hidden" name="registered" value="true" />
            <table>
                <tr>
                    <td>Teamname <input type="text" name="team_name" maxlength="70" onkeyup="CheckTeamName(<?php echo '\'' . $shortcut . '\'' ?>);" /></td>
                    <td id="check_teamname"></td>
                </tr>
                <tr>
                    <td>Passwort <input type="password" name="team_pw" maxlength="25" /></td>
                    <td id="check_teampw"></td>
                </tr>
            </table>
            <input type="hidden" name="team_shortcut" value="<?php echo $shortcut ?>" />
            <input type="submit" name="create" value="Team erstellen" />
        </form>
        <a href="index.php?id=<?php echo $id ?>">Zur Anmeldung</a>
        <?php
            }
            else
            {
                echo 'Ung&uuml;ltige Serien ID.';
            }
        }
        else
        {
            echo 'Keine Serien ID.';
        }
        ?>
    </body>
</html>
